<include file="Public/header"/>
<include file="Public/menu"/>

<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            首页
            <small>采购</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('Manage/index/index')}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="{:U('vendor/index')}"><i class="fa fa-dashboard"></i> 采购</a></li>
            <li class="active">我的采购计划</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">

        <div class="tab-content">
            <div class="tab-pane active" id="tab_0">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">我的采购计划</h3>
                        <div class="form-inline pull-right">
                            <form action="{:U('purchase/shoppingCart',array('order_field'=>'product_id','order'=>$order), '')}" method="get" pjax-container="">
                                <fieldset>
                                    <select class="form-control" name="search_field" style="height: 30px; font-size: 12px;">
                                        <option value="name" <if condition="$search_field eq 'name'">selected="selected"</if>>货品名称</option>
                                        <option value="barcode" <if condition="$search_field eq 'barcode'">selected="selected"</if>>条码</option>
                                        <option value="bn" <if condition="$search_field eq 'bn'">selected="selected"</if>>BN</option>
                                    </select>
                                    <div class="input-group input-group-sm">
                                        <div class="input-group input-group-sm">
                                            <input type="text" class="form-control" placeholder="" name="search" value="{$search}">
                                        </div>
                                    </div>

                                    <div class="input-group input-group-sm">
                                        <div class="input-group-btn">
                                            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>
                                            </button>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                        </div>

                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover table-view-list">
                            <tbody>
                            <tr>
                                <th><input type="checkbox" class="grid-select-all"></th>
                                <th>货品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/shoppingCart',array('p'=>$p,'order_field'=>'product_id','order'=>$order), '')}"></a></th>
                                <th>商品ID<a class="fa fa-fw fa-sort" href="{:U('purchase/shoppingCart',array('p'=>$p,'order_field'=>'goods_id','order'=>$order), '')}"></a></th>
                                <th>图片</th>
                                <th>名称</th>
                                <th>货品描述</th>
                                <th>条码</th>
                                <th>BN</th>
                                <th>采购价</th>
                                <th>采购数量</th>
                                <th>小计</th>
                                <th>操作</th>
                            </tr>
                            <volist name="list" id="vo">
                                <tr class="cg-product-tr yc-srarea">
                                    <td><input type="checkbox" class="grid-item" data-id="{$vo.product_id}" onclick="renderPrice(3, this)" ></td>
                                    <td>{$vo.product_id}</td>
                                    <td>{$vo.goods_id}</td>
                                    <!--<td><img src="{$vo.product_id|getThumb=###}" alt=""></td>-->
                                    <td><img src="{$vo.goods_id|getThumb=###}" data-bimg="{$vo.goods_id|getThumb=###}" alt="" class="scale-img"></td>
                                    <td><a href="javascript:;" class="short-gname" title="{$vo.name}">{$vo.name}</a></td>
                                    <td>{$vo.spec_info}</td>
                                    <td>{$vo.barcode}</td>
                                    <td>{$vo.bn}</td>
                                    <td><span class="badge bg-green grid-price" data-val="{$vo.purc_price|number_format=###, 2, '.', ''}">{$vo.purc_price|number_format=###, 2, '.', ''}</span></td>
                                    <td>
                                        <input name="purc_sum" class="form-control grid-num" data-min="{$vo.min_sum}" data-val="{$vo.purc_sum}" type="text" value="{$vo.purc_sum}" />
                                    </td>
                                    <td><span class="badge grid-total bg-blue">计算中...</span></td>
                                    <td>
                                        <a class="btn btn-sm btn-danger row-delete" data-id="{$vo.id}">移出</a>
                                    </td>
                                </tr>
                            </volist>
                            </tbody>
                        </table>
                    </div>
                    <div class="box-body table-responsive no-padding">
                        <div class="box-header">
                            <div class="form-inline pull-right">
                                <h3 class="box-title">选中<span id="total-num">0</span>个货品</h3>
                                <h3 class="box-title">总价:<span id="total-fee">0.00</span></h3>
                            </div>
                        </div>
                    </div>
                    <div class="box-footer clearfix">
                        <input type="checkbox" class="grid-select-all">&nbsp;&nbsp;&nbsp;
                        <a class="btn btn-sm btn-info batch-submit">提交采购单</a>
                        <a class="btn btn-sm btn-primary grid-refresh"><i class="fa fa-refresh"></i></a>
                        {$page}
                    </div>
                    <!-- /.box-body -->
                </div>

                <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
                     aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-hidden="true">×
                                </button>
                                <h4 class="modal-title" id="myModalLabel">
                                    填写备注
                                </h4>
                            </div>
                            <div class="modal-body">
                                <form action="" role="form" id="purchase-mark">
                                    <div class="form-group">
                                        <label for="exampleInputEmailbz">备注</label>
                                        <textarea class="form-control" rows="2" id="exampleInputEmailbz" placeholder="添加备注" name="desc"></textarea>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                    关闭
                                </button>
                                <button type="button" class="btn btn-primary  mark-submit">
                                    提交采购单
                                </button>
                            </div>
                        </div><!-- /.modal-content -->
                    </div><!-- /.modal-dialog -->
                </div><!-- /.modal -->
            </div>

        </div>
    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>
<script src="/Public/layer/layer.js"></script>
<script type="text/javascript">
	/*价格区域开始*/
	var rowCountList = [];
	var rowCheckNum = 0;
	var tbTotalPrice = "0.00";
	var tbNumObj = $("#total-num");
	var tbSumObj = $("#total-fee");
	// sid 0:初始化 1:全选 2:取消全部 3:取消选中单个
	function renderPrice(sid, curObj){
		if(sid != 3){
			rowCheckNum = 0;
		}					
		if(sid == 0){
			$(".grid-item").each(function(i, o){
				$(o).prop("checked", true);
				$(o).attr("data-index", i);
				rowCheckNum++;
			});
			$('.grid-select-all').prop("checked", true);
		}else if(sid == 1){
			$(".grid-item").each(function(i, o){
				if($(o).prop("checked")){
					rowCheckNum++;
				}
			});
		}					
		if(sid == 0 || sid == 1){
			tbTotalPrice = "0.00";
			$(".yc-srarea").each(function(i, o){
				if(sid == 0){
					rowCountList.push($(this));
				}
				var num = $(this).find(".grid-num").attr("data-val");
				var price = $(this).find(".grid-price").attr("data-val");
				var ctotal = getFixedFloat(price, num, "*");
				tbTotalPrice = getFixedFloat(tbTotalPrice, ctotal, "+");
				$(this).find(".grid-total").html(ctotal);
			});
		}else if(sid == 3){
			var o = $(curObj);
			var isMinus = false;	
			if(o.prop("checked")){
				rowCheckNum++;
			}else{
				rowCheckNum--;
				isMinus = true;
			}
			var parent = rowCountList[parseInt(o.attr("data-index"))];
			var num = parent.find(".grid-num").attr("data-val");
			var price = parent.find(".grid-price").attr("data-val");
			var ctotal = getFixedFloat(price, num, "*");
			tbTotalPrice = getFixedFloat(tbTotalPrice, ctotal, isMinus ? "-" : "+");
		}else{
			tbTotalPrice = "0.00";
		}		
		tbNumObj.html(rowCheckNum);
		tbSumObj.html(tbTotalPrice);
	}
	renderPrice(0);
	
    $(function () {
       function delgoods(data) {
            $.ajax({
                type: "POST",
                url: "{:U('purchase/cartdel')}",
                data: {data:data},
                success: function(msg){
                    if (msg.status) {
                        alert(msg.message);
                        window.location.reload();
						GPopup.alertTip(msg.message);
						setTimeout(function(){
							window.location.href= "{:U('purchase/shoppingList')}";	
						}, 1000);
                    } else {
                        alert(msg.message);
                    }
                },
                error: function (msg) {
                    alert("服务器异常,请稍后重试!");
                }
            });
        }        

        function submitProducts(data, mark) {
			GPopup.loadDialog();
            $.ajax({
                type: "POST",
                url: "{:U('purchase/cartSubmit')}",
                data: {data:data, mark:mark},
                success: function(msg){
					GPopup.removeLoad();
                    if (msg.status) {
                        GPopup.alertTip(msg.message);
						setTimeout(function(){
							window.location.href= "{:U('purchase/shoppingList')}";	
						}, 1000);
                    } else {
                        GPopup.alertTip(msg.message)
                    }
                    
                },
                error: function (msg) {
					GPopup.removeLoad();
                    GPopup.alertTip("服务器异常，请稍后重试！");
                }
            });
        }	
        $(".grid-num").each(function(i, o){
            $(this).on("change", function(){
				var v = $(this).val();
				var mval = parseInt($(this).attr("data-min"));
				var onum = parseInt($(this).attr("data-val"));
				if(isNaN(v)){
					$(this).val(onum);
					return;
				}else if(parseInt(v) <= mval){
					v = mval;
					$(this).val(v);
					$(this).attr("data-val", v);
				}
                var num = parseInt($(this).val());
                var price = rowCountList[i].find(".grid-price").attr("data-val");				
                var diff = num - onum;
                if(diff != 0){
				   var ctotal = getFixedFloat(price, num, "*");
                   rowCountList[i].find(".grid-total").html(ctotal);
                   var diffTotal = getFixedFloat(price, Math.abs(diff), "*");
                   tbTotalPrice = getFixedFloat(tbTotalPrice, diffTotal, diff > 0 ? "+" : "-");
                   tbSumObj.html(tbTotalPrice);
                   $(this).attr("data-val", num);
                }
            });
        });
		
		$('.grid-select-all').change(function () {
            if ($(this).prop("checked")) {
                $('.grid-item').prop("checked", true);
				renderPrice(1);
            } else {
                $('.grid-item').prop("checked", false);
				renderPrice(2);
            }
        });

        //删除一个购物车中的产品
        $('.row-delete').click(function () {
            if(!confirm("确认移除该货品?")){
                return false;
            }
            var id = $(this).attr('data-id');
            var data = {id:id};
            if (data) {
                delgoods(data);
            }
        });
		$('.batch-submit').click(function () {
            var data = [];
            $(".grid-item").each(function (i) {
                if ($(this)[0].checked) {
                    var product_id = $(this).attr('data-id');
                    var purc_sum = $(this).parent().parent().find('input[name=purc_sum]').val();
                    data.push({product_id:product_id,purc_sum:purc_sum});
                }
            });
            if (!data.length) {
                alert('请至少选择一个');
                return false;
            }
            $('#myModal').modal('show');
        });

        $('.mark-submit').click(function () {
            var data = [];
            $(".grid-item").each(function (i) {
                if ($(this)[0].checked) {
                    var product_id = $(this).attr('data-id');
                    var purc_sum = $(this).parent().parent().find('input[name=purc_sum]').val();
                    data.push({product_id:product_id,purc_sum:purc_sum});
                }
            });
            var mark = $('#exampleInputEmailbz').val();
            submitProducts(data, mark);
        });
		/*价格区域束*/
		showBigPicture();
    });
</script>